package com.hkbigdata.sybx.controller;

import com.hkbigdata.sybx.bean.Image;
import com.hkbigdata.sybx.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuanbo
 * @creat 2025-05-30-13:28
 * @see 2194550857@qq.com
 */
    @RestController
    @RequestMapping("/api/image")
public class ImageController {
    @Autowired
    private ImageService imageService;
//    @PostMapping("/insert")
//    public Map<String,Object> insertImage(@RequestBody Image image) {
//        boolean b = imageService.InsertImage(image);
//        Map<String, Object> response = new HashMap<>();
//        response.put("success", b);
//        return response;
//    }
//
////    @GetMapping("/select")
////    public Image selectImage(@RequestParam("userId") String userId) {
////        Image image = imageService.SelectImage(userId);
////        return image;
////    }
//
//    @GetMapping("/select")
//    public ResponseEntity<Map<String, Object>> selectImage(@RequestParam("userId") String userId) {
//        Image image = imageService.SelectImage(userId);
//
//        if (image != null) {
//            // 将二进制数据转换为Base64
//            String base64Image = Base64.getEncoder().encodeToString(image.getImageData());
//
//            Map<String, Object> result = new HashMap<>();
//            result.put("success", true);
//            result.put("data", new HashMap<String, Object>() {{
//                put("imageData", base64Image);
//                put("contentType", image.getContentType());
//            }});
//            return ResponseEntity.ok(result);
//        } else {
//            return ResponseEntity.ok(new HashMap<String, Object>() {{
//                put("success", false);
//                put("message", "未找到用户头像");
//            }});
//        }
//    }

    @GetMapping("/select")
    public ResponseEntity<Map<String, Object>> selectImage(@RequestParam("userId") String userId) {
        Image image = imageService.SelectImage(userId); // 注意方法名规范（驼峰命名）

        if (image != null && image.getImageData() != null) {
            // 将二进制数据转换为Base64
            String base64Image = Base64.getEncoder().encodeToString(image.getImageData());

            Map<String, Object> data = new HashMap<>();
            data.put("imageData", base64Image);
            data.put("contentType", image.getContentType() != null ? image.getContentType() : "image/png");

            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("data", data);
            result.put("message", "获取头像成功");
            return ResponseEntity.ok(result);
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "未找到用户头像");
            return ResponseEntity.ok(result);
        }
    }

    // 新增：修改图片的API接口
    @PostMapping("/insert")
    public ResponseEntity<Map<String, Object>> insertImage(@RequestBody Image image) {
        try {
            // 假设imageService.insertImage返回布尔值表示是否插入成功
            boolean success = imageService.InsertImage(image);

            Map<String, Object> result = new HashMap<>();
            result.put("success", success);

            if (success) {
                // 插入成功后直接返回新头像数据（可选）
                String base64Image = Base64.getEncoder().encodeToString(image.getImageData());
                Map<String, Object> data = new HashMap<>();
                data.put("imageData", base64Image);
                data.put("contentType", image.getContentType() != null ? image.getContentType() : "image/png");
                result.put("data", data);
                result.put("message", "头像上传成功");
            } else {
                result.put("message", "头像上传失败");
            }

            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "服务器错误，头像上传失败");
            result.put("error", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
    // 新增：修改图片的API接口
    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateImage(@RequestBody Image image) {
        try {
            // 检查用户头像是否存在
            Image existingImage = imageService.SelectImage(image.getUserId());
            if (existingImage == null) {
                Map<String, Object> result = new HashMap<>();
                result.put("success", false);
                result.put("message", "用户头像不存在，无法修改");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);
            }

            // 调用服务层更新头像
            boolean success = imageService.updateImage(image);

            Map<String, Object> result = new HashMap<>();
            result.put("success", success);

            if (success) {
                // 更新成功后返回新头像数据
                String base64Image = Base64.getEncoder().encodeToString(image.getImageData());
                Map<String, Object> data = new HashMap<>();
                data.put("imageData", base64Image);
                data.put("contentType", image.getContentType() != null ? image.getContentType() : "image/png");
                result.put("data", data);
                result.put("message", "头像更新成功");
            } else {
                result.put("message", "头像更新失败");
            }

            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "服务器错误，头像更新失败");
            result.put("error", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

}
